/**
 * @Author: 安广伟
 * @Email: anguangwei@jwzh.tech
 * @Date: 2025-03-19 16:45:44
 * @LastEditTime 2025-03-19 16:45:44
 * @since: 1.0.0
 * @Version: 1.0.0
 * @Description:
 */

package com.hmall.api.config;

import com.hmall.api.client.fallback.ItemClientFallback;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig {
    @Bean
    public Logger.Level feignLogLevel() {
        return Logger.Level.FULL;
    }

    /**
     * feign拦截器，将用户信息传递给服务端
     * @return
     */
    @Bean
    public RequestInterceptor userInfoRequestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                Long user = UserContext.getUser();
                if (user != null){
                    template.header("user-info", user.toString());
                }
            }
        };
    }

    @Bean
    public ItemClientFallback itemClientFallback() {
        return new ItemClientFallback();
    }
}
